//
//  DYEmptyView.swift
//  QuRongXiang
//
//  Created by qurongxiang on 2018/6/25.
//  Copyright © 2018年 zhaowang. All rights reserved.
//

import UIKit

enum EmptyType {
    case emptyData //空数据
    case networkError //网络请求失败
}

protocol EmptyDataSetProtocol { }

extension EmptyDataSetProtocol where Self : UIViewController {
    
    func addEmptyView(type: EmptyType? = .emptyData , toView:UIView, imgName: String, tipTitle: String, action: Selector? = nil) {
        
        let emptyView = UIView()
        emptyView.tag = 1024
        
        //父视图
        toView.addSubview(emptyView)
        emptyView.translatesAutoresizingMaskIntoConstraints = false
        emptyView.leftAnchor.constraint(equalTo: toView.leftAnchor).isActive = true
        emptyView.rightAnchor.constraint(equalTo: toView.rightAnchor).isActive = true
        emptyView.topAnchor.constraint(equalTo: toView.topAnchor).isActive = true
        emptyView.bottomAnchor.constraint(equalTo: toView.bottomAnchor).isActive = true
        //emptyView.autoresizingMask  = [.flexibleBottomMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleLeftMargin]
        emptyView.backgroundColor = UIColor.white
        
        //说明文字
        let label = UILabel()
        label.text = tipTitle
        emptyView.addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        let labelCenterY = label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        labelCenterY.isActive = true
        label.textColor = UIColor.blue
        
        //按钮
        let btn = UIButton(type: .custom)
        btn.setTitle("刷新数据", for: .normal)
        emptyView.addSubview(btn)
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        let btnCenterY = btn.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        btnCenterY.isActive = true
        btn.widthAnchor.constraint(equalToConstant: 100).isActive = true
        btn.heightAnchor.constraint(equalToConstant: 40).isActive = true
        btn.backgroundColor = UIColor.white
        btn.setTitleColor(UIColor.blue, for: .normal)
        btn.layer.cornerRadius = 20
        btn.layer.borderColor = UIColor.darkGray.cgColor
        btn.layer.borderWidth = 0.5
        btn.clipsToBounds = false
        if (action != nil) {
            btn.addTarget(self, action: action!, for: .touchUpInside)
        }
        
        //图片
        let imgView = UIImageView()
        imgView.image = UIImage(named: "emptyWifi")
        emptyView.addSubview(imgView)
        imgView.translatesAutoresizingMaskIntoConstraints = false
        imgView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        let imgViewCenterY = imgView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        imgViewCenterY.isActive = true
        imgView.widthAnchor.constraint(equalToConstant: 200).isActive = true
        imgView.heightAnchor.constraint(equalToConstant: 200).isActive = true
        
        
        if type == EmptyType.emptyData {
            btn.isHidden = true
            btnCenterY.constant = 0
            labelCenterY.constant = 20
            imgViewCenterY.constant = -100
        }else {
            btn.isHidden = false
            btnCenterY.constant = 40
            labelCenterY.constant = 90
            imgViewCenterY.constant = -100
        }
        
    }
}


extension UIViewController :EmptyDataSetProtocol {
    
    /// 添加空页面
    ///
    /// - Parameters:
    ///   - tipTitle: 标题
    ///   - imgName: 图片
    ///   - toView: 添加到哪个页面
    func showEmptyDataView (tipTitle:String = "没有数据" ,  imgName:String = "" ,toView:UIView? = nil ) {
        guard let tempView = toView == nil ? self.view : toView else { return }
        addEmptyView(type: .emptyData, toView: tempView, imgName: imgName, tipTitle: tipTitle, action: nil)
    }
    
    /// 移除页面
    func hideEmptyView() {
        view.subviews.filter({ $0.tag == 1024 }).first?.removeFromSuperview()
    }
    
    /// 网络异常
    ///
    /// - Parameters:
    ///   - tipTitle: 标题
    ///   - imgName: 图片
    ///   - toView: 添加到哪个View
    ///   - action: 点击事件
    func showNetworkErrorView(tipTitle:String = "网络异常" ,  imgName:String = "" ,toView:UIView? = nil , action: Selector? = nil ) {
        guard let tempView = toView == nil ? self.view : toView else { return }
        addEmptyView(type: .networkError, toView: tempView, imgName: imgName, tipTitle: tipTitle, action: action)
    }
    
    /// 添加空页面
    ///
    /// - Parameters:
    ///   - tipTitle: 标题
    ///   - imgName: 图片
    ///   - toView: 添加到哪个页面
    func showInputEmptyView (tipTitle:String = "输入内容为空" ,  imgName:String = "" ,toView:UIView? = nil ) {
        guard let tempView = toView == nil ? self.view : toView else { return }
        addEmptyView(type: .emptyData, toView: tempView, imgName: imgName, tipTitle: tipTitle, action: nil)
    }
}


